package com.lc.recovery.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.LoginApi;
import com.lc.recovery.http.response.LoginBean;
import com.lc.recovery.manager.ActivityManager;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;
import com.lc.recovery.util.constant.EvenConstant;
import com.lc.recovery.util.eventbus.Event;
import com.lc.recovery.util.eventbus.EventBusUtils;
import com.lc.widget.view.ClearEditText;
import com.lc.widget.view.PasswordEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginPassActivity extends AppActivity {
    @BindView(R.id.et_login_phone)
    ClearEditText etLoginPhone;
    @BindView(R.id.et_login_password)
    PasswordEditText etLoginPassword;

    @BindView(R.id.tv_password_login)
    TextView tvPasswordLogin;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_loginpass;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);


    }

    @OnClick({R.id.tv_register, R.id.tv_login_forget, R.id.btn_login_commit, R.id.tv_password_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_register:
                startActivity(RegisterActivity.class);
                break;
            case R.id.tv_login_forget:
                startActivity(PasswordForgetActivity.class);
                break;
            case R.id.btn_login_commit:
                if (etLoginPhone.getText().toString().length() != 11) {

                    toast(R.string.common_phone_input_error);
                    return;
                }

                EasyHttp.post(this)
                        .api(new LoginApi()
                                .setMobile(etLoginPhone.getText().toString())
                                .setPassword(etLoginPassword.getText().toString()))
                        .request(new HttpCallback<LoginBean>(this) {



                            @Override
                            public void onSucceed(LoginBean data) {
                                 if (data.getCode()==200){
                                     SPUtil.put(LoginPassActivity.this, IntentKey.USERID,data.getUser().getId());
                                     SPUtil.put(LoginPassActivity.this, IntentKey.TYPE,data.getUser().getType()+"");

                                     // 进行内存优化，销毁除登录页之外的所有界面
                                     ActivityManager.getInstance().finishAllActivities(HomeActivity.class);
                                     EventBusUtils.sendEvent(new Event(EvenConstant.TYPE, data.getUser().getType()));
                                     startActivity(HomeActivity.class);

                                 }else {

                                   toast(data.getMsg());

                                 }


                            }

                            @Override
                            public void onFail(Exception e) {
//                                super.onFail(e);


                            }
                        });
                break;
            case R.id.tv_password_login:

                startActivity(LoginActivity.class);
finish();
                break;
        }
    }
}
